/*
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2014
 *
 * fileName:  PictureDefault.java
 * author:  lei
 * Company:  Cloudland Studio
 * Createdate:  2013-9-15 上午11:05:18
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2013-9-15          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.picture.support;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.cloudland.picture.IPictureSign;

 /**
 * <pre> 
 * 添加文字、图片水印默认实现类。开发人员可根据自己添加水印的要求，单独实现个别效果。
 * </pre>
 * @ClassName  PictureDefault
 * @author  lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class PictureDefault implements IPictureSign {

	/**
     * alpha 透明度 取值0-1, 默认值: 0.3
     */
    private float alpha = 0.3f;
    
    /**
     * degree 旋转角度
     */
    private double degree = 0;
    
    /**
     * content 默认水印内容
     */
    private String content = "Cloudland";
    
     /**
     * <pre> 
     * 水印类型枚举
     * </pre>
     * @ClassName  SingType
     * @author  lei
     * @version  %I%, %G%
     * @see  
     * @since   JDK1.6
     *
     */
    public static enum SingType {
        CONTEXT, ICON;
    };
    
    /**
     * type 水印类型
     */
    private SingType type;
    
    /**
     * file 水印图片
     */
    private File file;
    
    public PictureDefault(SingType type, String content) {
        this.type = type;
        this.content = content;
    }
    
    public PictureDefault(SingType type, File iconF) {
        this.type = type;
        this.file = iconF;
    }
    
    public void doSign(Graphics grap, int canvasWidth, int canvasHeight)
    {
        if (SingType.CONTEXT.equals(type)) {
            doSignContent(grap, canvasWidth, canvasHeight);
        } else {
            try
            {
                doSignIcon(grap, canvasWidth, canvasHeight);
            }
            catch (IOException e)
            {
                // TODO 读取做水印的图片失败。请确保此文件已存在，并且是一张图片。
                new RuntimeException("", e);
            }
        }
    }
    
    /**
     * <pre>
     * 添加文字水印。
     * </pre>
     *
     * @param grap
     * @param canvasWidth
     * @param canvasHeight   
     */
    protected void doSignContent(Graphics grap, int canvasWidth, int canvasHeight)
    {
        Graphics2D g = (Graphics2D)grap;
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setColor(Color.WHITE);
        int fontSize = 30;
        g.setFont(new Font("黑体", Font.BOLD, fontSize));
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.3f));

        //设置水印旋转   
        g.rotate(Math.toRadians(degree), canvasWidth/2 - 20, canvasHeight/2 - 20);
        int x = (canvasWidth - 20) - (getLength(content) * (fontSize/2));
        int y = (canvasHeight - 20) - fontSize/2;
        
        // 居中设置
        //x = (canvasWidth - (getLength(content) * fontSize))/ 2;
        //y = (canvasHeight - fontSize) / 2;
        // 绘制文字
        g.drawString(content,  x,  y);
        g.dispose();
    }
    
    /**
     * <pre>
     * 添加图片水印。
     * </pre>
     *
     * @param grap
     * @param canvasWidth
     * @param canvasHeight
     * @throws IOException   
     */
    protected void doSignIcon(Graphics grap, int canvasWidth, int canvasHeight) throws IOException {
        Graphics2D g = (Graphics2D)grap;
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        
        //水印图象的路径 水印一般为gif或者png的，这样可设置透明度
        Image img = ImageIO.read(file);
        //float alpha = 0.5f; //透明度
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
        
        //设置水印旋转   
        g.rotate(Math.toRadians(degree), (canvasWidth - 20) - (img.getWidth(null)), (canvasHeight - 20) - img.getHeight(null));
        
        g.drawImage(img, (canvasWidth - 20) - (img.getWidth(null)), (canvasHeight - 20) - img.getHeight(null), null);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        g.dispose();
    }
    
    /**
     * <pre>
     * 计算text的长度（一个中文算两个字符）
     * </pre>
     *
     * @param content
     * @return   
     */
    private int getLength(String content)
    {
        int length = 0;
        for (char c : content.toCharArray()) {
            if (String.valueOf(c).getBytes().length > 1) {
                length += 2;
                continue;
            }
            length++;
        }
        return length;
    }
}
